<?php
/**
 * Created by PhpStorm.
 * User: qttx
 * Date: 2019/4/15
 * Time: 15:24
 */
namespace app\api\controller;

use think\Controller;
use think\Db;

class Message extends Controller
{
    //获取accesstoken

    public function getAccessToken($appid,$secret){

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";

        $res = $this->curl_get($url);

        $res = json_decode($res,1);

        return $res['access_token'];

    }

//获取模板消息内容主体

//因为是测试所以写死，大家可以通过传参的方式获取

    public function getMsg($appid,$secret,$openid,$template_id,$form_id,$name,$phone,$content,$emphasis_keyword='keyword1'){
        $time = date('Y-m-d',time());

        $data['data']= [
            'keyword1'=>['value'=>$name,'color'=>''],
            'keyword2'=>['value'=>$phone,'color'=>''],
            'keyword3'=>['value'=>$time,'color'=>''],
            'keyword4'=>['value'=>$content,'color'=>''],
        ];//内容主体
        $data['touser'] = $openid;//用户的openid

        $data['template_id'] = $template_id;//从微信后台获取的模板id

        $data['form_id'] = $form_id;//前端提供给后端的form_id

        $data['page'] = 'pages/index/index';//小程序跳转页面

        $data['emphasis_keyword'] = $emphasis_keyword;//选择放大的字体
        return $data;

    }

    public function send($appid,$secret,$openid,$template_id,$form_id,$name,$phone,$content){
        $access_token = $this->getAccessToken($appid,$secret);
        $send_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
        $data = $this->getMsg($appid,$secret,$openid,$template_id,$form_id,$name,$phone,$content);
        $str = $this->curl_post($send_url,json_encode($data));
        $str = json_decode($str,1);
        return $str;

    }

    public function curl_post($url, $fields, $data_type='text')

    {

        $cl = curl_init();

        if(stripos($url, 'https://') !== FALSE) {

            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($cl, CURLOPT_SSLVERSION, 1);

        }

        curl_setopt($cl, CURLOPT_URL, $url);

        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );

        curl_setopt($cl, CURLOPT_POST, true);

        curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);

        $content = curl_exec($cl);

        $status = curl_getinfo($cl);

        curl_close($cl);

        if (isset($status['http_code']) && $status['http_code'] == 200) {

            if ($data_type == 'json') {

                $content = json_decode($content);

            }

            return $content;

        } else {

            return FALSE;

        }

    }

    public function curl_get($url, $data_type='text')

    {

        $cl = curl_init();

        if(stripos($url, 'https://') !== FALSE) {

            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($cl, CURLOPT_SSLVERSION, 1);

        }

        curl_setopt($cl, CURLOPT_URL, $url);

        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );

        $content = curl_exec($cl);

        $status = curl_getinfo($cl);

        curl_close($cl);

        if (isset($status['http_code']) && $status['http_code'] == 200) {

            if ($data_type == 'json') {

                $content = json_decode($content);

            }

            return $content;

        } else {

            return FALSE;

        }

    }
    public function index($openid,$name,$phone,$content,$formid)
    {
        $appid = 'wx6c1a86ee3e43e4a3';//小程序appid

        $openid = $openid;//接收用户的openid

        $template_id  = 'fNvaDpQqnOYWLc10i6S_luZdSeQ_aKVn9YF4jp5r7Y0';//从微信后台获取的模板id

        $form_id = $formid;//七天内的formid

        $secret = '32f54b415ab46d898b2714c327b8d4b9';//密钥
        $data = $this->send($appid,$secret,$openid,$template_id,$form_id,$name,$phone,$content);
        return $data;//打印测试结果

    }
}